home *** CD-ROM | disk | FTP | other *** search
/ BUG 1 / BUGCD1996_0708.ISO / pc / util / minilin / minilin.exe / USR / SBIN / MAKEWHAT.{__ < prev    next >
Text File  |  1994-04-25  |  7KB  |  183 lines

  1. #!/bin/sh
  2. # makewhatis: create the whatis database
  3. # Created: Sun Jun 14 10:49:37 1992
  4. # Revised: Wed Dec 23 14:22:44 1992 by root
  5. # Copyright 1992 Rickard E. Faith (faith@cs.unc.edu)
  6. # May be freely distributed and modified as long as copyright is retained.
  7.  
  8. # Wed Dec 23 13:27:50 1992: Rik Faith (faith@cs.unc.edu) applied changes
  9. # based on Mitchum DSouza (mitchum.dsouza@mrc-apu.cam.ac.uk) cat patches.
  10. # Also, cleaned up code and make it work with NET-2 doc pages.
  11.  
  12. # Thu Sep 23 03:42:49 GMT 1993
  13. # Modified to work better with .gz, .z, and more garbage resistant
  14. # by: Mark Lord (mlord@bnr.ca)
  15.  
  16. # Sun Apr 24 21:05:08 CDT 1994
  17. # Modified to look for preformatted cat pages in /usr/man/preformat,
  18. # and to update an existing database correctly by Patrick Volkerding
  19. # (volkerdi@ftp.cdrom.com)
  20.  
  21. PATH=/usr/bin:/bin
  22.  
  23. for name in $*
  24. do
  25. case $name in
  26.     -a) if echo $* | grep '\-u' 1> /dev/null 2> /dev/null ; then
  27.         # a little recursion never hurt anybody... :^)
  28.         touch --file=/usr/man/whatis /tmp/timeref$$
  29.         $0 -u
  30.         # and there's nothing wrong with an occasional kludge. ;^)
  31.         # If you plan to build the whatis database in a different place
  32.         # than /usr/man, you'll have to change the 3 'touch' lines to
  33.         # make the kludge for '-a' work correctly. Maybe someday I'll
  34.         # fix it, but for now it's a big improvement from the way it 
  35.             # was before, so I'm gonna be lazy.
  36.         touch --file=/tmp/timeref$$ /usr/man/whatis
  37.         rm -f /tmp/timeref$$
  38.         $0 -c -u
  39.     else
  40.         $0
  41.         touch -t 010100001970 /usr/man/whatis
  42.         $0 -c -u
  43.     fi
  44.     exit;;
  45.     -u) update="-anewer /usr/man/whatis";
  46.         continue;;
  47.     -c) pages=cat;
  48.         filter="col -bx";
  49.     mandir=/usr/man/preformat
  50.         continue;;
  51.     -*) echo "Usage: makewhatis -a -c -u [manpath]";
  52.     echo "       -a: Builds a database of all pages in /usr/man/man* and"
  53.     echo "           /usr/man/preformat (do not mix with -c)"
  54.         echo "       -c: build database from cat pages in /usr/man/preformat/cat*";
  55.     echo "           (otherwise database will be extracted from groff source"
  56.     echo "           pages found in /usr/man/man*)"
  57.         echo "       -u: update database with man/cat pages that are newer than the";
  58.     echo "           existing /usr/man/whatis"
  59.         echo "       [manpath]: man directory (default: /usr/man)";
  60.         exit;;
  61.      *) if [ -d ${name} ]
  62.         then
  63.             mandir=$name
  64.         else
  65.             echo "No such directory $name"
  66.             exit
  67.         fi;;
  68. esac
  69. done
  70.  
  71. pages=${pages-man}
  72. export pages
  73. mandir=${mandir-/usr/man}
  74. filter=${filter-cat}
  75.  
  76. cd $mandir
  77.  
  78. for i in 0 1 2 3 4 5 6 7 8 9 n l
  79. do
  80.     if [ -d ${pages}$i ]
  81.     then
  82.         cd ${pages}$i
  83.         section=$i
  84.         export section
  85.         for j in `find . -name '*' ${update} -print`
  86.         do
  87.         Cat=cat
  88.         if [ `basename $j .gz` != `basename $j` -o \
  89.         `basename $j .z` != `basename $j` -o \
  90.         `basename $j .Z` != `basename $j` ]; then
  91.                 Cat=zcat
  92.             fi
  93.             ${Cat} ${j} | ${filter} |\
  94.             gawk 'BEGIN {after = 0; insh = 0; wordjoin = 1;
  95.                         pages = ENVIRON["pages"];
  96.                         section = ENVIRON["section"]} {
  97.                 if (($1 ~ /^\.[Ss][Hh]/ && $2 ~ /[Nn][Aa][Mm][Ee]/) ||
  98.                     (pages == "cat" && $1 ~ /^NAME/)) {
  99.                     if (!insh)
  100.                         insh = 1
  101.                     else {
  102.                         printf "\n"
  103.                         exit
  104.                     }
  105.                 } else if (insh) {
  106.                     if ($1 ~ /^\.[Ss][HhYS]/ ||
  107.                         (pages == "cat" && $1 ~ /^[DS][yYeE]/)) {
  108.                         printf "\n"
  109.                         exit
  110.                     } else { # Substitutions after Tom Christiansen perl script
  111.             if (!wordjoin)
  112.                 printf " "
  113.                         gsub(/    /, " ")            # tabs to spaces
  114.                         sub(/^ +/, "")            # Kill initial spaces
  115.             if (!after && $0 ~ /^[^ ]+-/)
  116.                 sub(/-/, " - ")
  117.                         sub(/  *,/,",")            # Fix comma spacings
  118.                         sub(/,/,", ")            # Fix comma spacings
  119.                         sub(/--/," - ")            # Handle Double dash
  120.                         gsub(/  */, " ")        # Collapse spaces
  121.                         sub(/  *$/, "")            # Kill trailing spaces
  122.             if ($0 ~ /[^ ]-$/) {
  123.                             sub(/-$/, "")        # Hypenation
  124.                 wordjoin = 1
  125.             } else
  126.                 wordjoin = 0
  127.                         sub(/^.[IB] /, "")        # Kill bold and italics
  128.                         gsub(/\\f[PRIB0123]/, "")    # Kill font changes
  129.                         gsub(/\\s[-+0-9]*/, "")        # Kill size changes
  130.                         gsub(/\\&/, "")            # Kill \&
  131.                         gsub(/\\\((ru|ul)/, "_")    # Translate
  132.                         gsub(/\\\((mi|hy|em)/, "-")    # Translate
  133.                         gsub(/\\\*\(../, "")        # Kill troff strings
  134.                         sub(/^\.\\\"/, "")        # Kill comments
  135.                         gsub(/\\/, "")            # Kill all backslashes
  136.             if (after) {
  137.                                 if ($1 !~ /^\.../ && $1 != "")
  138.                                     printf "%s", $0
  139.                                 else {
  140.                                     printf "\n"
  141.                                     after = 0
  142.                                 }
  143.             } else if ($0 !~ / - / && $0 !~ / -$/ && $0 !~ /^- /) {
  144.                                 if ($1 !~ /^\.../ && $1 != "")
  145.                                     printf "%s", $0
  146.                                 else
  147.                                     printf "\n"
  148.                         } else {
  149.                             after = 1
  150.                             if ($0 ~ / - /) {
  151.                                 printf "%-20s", sprintf( "%s (%s)",
  152.                                     substr( $0, 0, match( $0, / - / )-1 ), section )
  153.                                 printf " - %s", toupper(substr( $0, 3 + match( $0, / - / ),1 ))
  154.                                 printf "%s", substr( $0, 4 + match( $0, / - / ) )
  155.                             } else if ($0 ~ / -$/) {
  156.                                 printf "%-20s", sprintf( "%s (%s) -",
  157.                                     substr( $0, 0, match( $0, / -$/ )-1 ), section )
  158.                             } else {
  159.                                 printf "(%s) %s", section, $0
  160.                             }
  161.                         }
  162.                     }
  163.                 }
  164.             }'
  165.             done
  166.         cd ..
  167.     fi
  168. done > /tmp/whatis$$
  169. if [ "$mandir" = "/usr/man/preformat" ]; then
  170.     # Another cheap hack to deal with /usr/man/preformat:
  171.     mandir=/usr/man
  172. fi
  173. if [ ! "$update" = "" ]; then
  174.     touch /usr/man/whatis
  175.     cp /usr/man/whatis /tmp/oldwhatis$$
  176.     cat /tmp/whatis$$ /tmp/oldwhatis$$ | sed '/^$/d' | sort | uniq > ${mandir}/whatis
  177.     rm -f /tmp/oldwhatis$$
  178. else
  179.     sed '/^$/d' < /tmp/whatis$$ | sort | uniq > ${mandir}/whatis
  180. fi
  181. chmod 644 ${mandir}/whatis
  182. rm /tmp/whatis$$
  183.